import 'package:easy_refresh/easy_refresh.dart';
import 'package:get/get_state_manager/src/simple/get_controllers.dart';
import 'package:largefriends/net/error_handle.dart';
import 'package:largefriends/utils/lf_dialog_utils.dart';

import '../net/lf_dio_util.dart';

enum LoadingState {
  init,
  loading,
  success,
  failed,
  empty,
}

abstract class LfBaseListController<T> extends GetxController {
  final EasyRefreshController refreshController = EasyRefreshController(
      controlFinishLoad: false, controlFinishRefresh: false);
  LoadingState state = LoadingState.init;
  int _page = 1;
  final int _offset = 20;
  Map<String, dynamic> requestParam = {};
  late List<T> dataList;

  getCustomRequestParam({bool isRefresh = false});

  Future<IndicatorResult> getCustomParamHttpRequest({bool isRefresh = false});

  /// 网络请求回来后先处理下数据,比如屏蔽无效的数据
  List<T> processData(List<T> data) {
    return data;
  }

  /// 添加到列表之前做处理,防止重复
  List<T> filterData(List<T> data) {
    List<T> result = List.empty(growable: true);
    for (var element in data) {
      var index = dataList
          .indexWhere((value) => filterKey(value: value, other: element));
      if (index < 0) {
        result.add(element);
      }
    }
    return data;
  }

  bool filterKey({required T value, required T other}) {
    return false;
  }

  ///最后的调用,
  onRequestFinish() {}

  Future<IndicatorResult> httpRequest(
      {bool refresh = false, required String httpUrl}) async {
    IndicatorResult indicatorResult = IndicatorResult.none;
    state = LoadingState.loading;
    if (dataList.isEmpty) {
      update();
    }
    if (_page < 1) {
      _page = 1;
    }
    requestParam.clear();
    getCustomRequestParam(isRefresh: refresh);
    requestParam['offset'] = _offset;
    requestParam['page'] = refresh ? 1 : _page;

    await LfDio().get<List<T>>(url: httpUrl, params: requestParam).then(
        (value) {
      var data = processData(value);
      if (refresh) {
        if (data.isEmpty) {
          state = LoadingState.empty;
        } else {
          dataList.clear();
          dataList.addAll(data);
          state = LoadingState.success;
          _page = 2;
        }
        indicatorResult = IndicatorResult.success;

        ///如果下拉刷新,重新reset一下no more,防止无法加载
      } else {
        if (data.isEmpty) {
          state = LoadingState.empty;
          indicatorResult = IndicatorResult.noMore;
        } else {
          dataList.addAll(filterData(data));
          state = LoadingState.success;
          _page++;
          indicatorResult = IndicatorResult.success;
        }
      }
      onRequestFinish();
      update();
    }, onError: (error) {
      if (error is NetError) {
        showSnackBar(error.msg);
      }
      state = LoadingState.failed;
      indicatorResult = IndicatorResult.fail;
      onRequestFinish();
      update();
    });
    return indicatorResult;
  }

  @override
  void onInit() {
    dataList = <T>[];
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    getCustomParamHttpRequest(isRefresh: true);
  }

  @override
  void onClose() {
    refreshController.dispose();
    super.onClose();
  }
}
